//related to picker3.ui
var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_Storage");
var nf = sm("do_Notification");
var root = ui("$");
var urlUser = require("url/user");
var qqHttp = require("helper/http");
var userUtil = require("helper/user");

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(360,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:0};
animPanelShow.append(360,propsPS,"EaseOut");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(360,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(360,propsPH,"EaseIn");

//picker
var labtit = ui("do_Label_2");
var pickerbg = ui("pickerbg");//遮罩
var pickerbox = ui("do_ALayout_1");

var gridSex = ui("gridSex");
var gridInterest = ui("gridInterest");
var gridIndustry = ui("gridIndustry");
var gridDelicacy = ui("gridDelicacy");
var gridHometown = ui("gridHometown");
var gridEntertainment = ui("gridEntertainment");
page.on("interests",function(data){
	pickerbg.visible = true;
	pickerbg.animate(animMaskShow);
	pickerbox.animate(animPanelShow);
})
var sexData=[],interestData=[],industryData=[],delicacyData=[],hometownData=[],entertainmentData=[];
var param= {"sexKey":"","interestKey":"","industryKey":"","delicacyKey":"","hometownKey":"","entertainmentKey":""};
//初始化

page.on("getInterests",function(data){
	var sexText = data.sexKey;
	var interestText = data.interestKey;
	var industryText = data.industryKey;
	var delicacyText = data.delicacyKey;
	var homeTownText = data.hometownKey;
	var entertainmentText = data.entertainmentKey;
	sexData = [{"name":"男","selected":0,"val":1},{"name":"女","selected":0,"val":2}];
    interestData = JSON.parse(userUtil.loadUserPara(1,'interest'));

        if(interestText!=''){
    		for(var i in interestData){
    			if(interestText.indexOf(interestData[i].name) >= 0){
    				interestData[i].selected = 1;
    				param.interestKey = interestData[i].name;
    			}
    			else{
    				interestData[i].selected = 0
    			}
    		}
    	}
        
    	gridInterest.items = interestData; //兴趣爱好
    	            
    	industryData = JSON.parse(userUtil.loadUserPara(1,'industry'));

		if(industryText!=''){
			for(var i in industryData){
				if(industryText == industryData[i]['val']){
					industryData[i].selected = 1;
					param.industryKey = industryData[i].val;
				}
				else{
					industryData[i].selected = 0
				}
			}
		}
		gridIndustry.items = industryData;
    	
    delicacyData = JSON.parse(userUtil.loadUserPara(1,'delicacy'));

		if(delicacyText!=''){
			for(var i in delicacyData){
				if(delicacyText.indexOf(delicacyData[i].name) >= 0){
					delicacyData[i].selected = 1;
					param.delicacyKey = delicacyData[i].name;
				}
				else{
					delicacyData[i].selected = 0
				}
			}
		}
		gridDelicacy.items = delicacyData;
    	
    hometownData = JSON.parse(userUtil.loadUserPara(1,'hometown'));

		if(homeTownText!=''){
			for(var i in hometownData){
				if(homeTownText == hometownData[i]['val']){
					hometownData[i].selected = 1;
					param.hometownKey = hometownData[i].val;
				}
				else{
					hometownData[i].selected = 0
				}
			}
		}
		gridHometown.items = hometownData;
    	
    entertainmentData = JSON.parse(userUtil.loadUserPara(1,'entertainment'));

		if(entertainmentText!=''){
			for(var i in entertainmentData){
				if(entertainmentText.indexOf(entertainmentData[i].name) >= 0){
					entertainmentData[i].selected = 1;
					param.entertainmentKey = entertainmentData[i].name;
				}
				else{
					entertainmentData[i].selected = 0
				}
			}
		}
		gridEntertainment.items = entertainmentData;

	
	if(sexText==1||sexText==2){
		for(var i in sexData){
			if(sexText==sexData[i].val){
				sexData[i].selected = 1;
				param.sexKey = sexData[i].val;
			}
			else{
				sexData[i].selected = 0
			}
		}
	}
	gridSex.items = sexData;  //性别
	
	
	
	
	
	
});
//遮罩事件
pickerbg.on("touch","",300,hideLayout);

//防穿透
pickerbox.on("touch",function(){})

//选择性别选项
gridSex.on("touch", function(data) {
	param.sexKey="";
	for(var i in sexData){
		if(data==i&&sexData[i].selected == 0){
			sexData[i].selected = 1;
			param.sexKey=sexData[i].val;
		}
		else{
			sexData[i].selected = 0
		}
	}
	gridSex.items = sexData;
});
//选择爱好选项
gridInterest.on("touch", function(data) {
	param.interestKey="";
	for(var i in interestData){
		if(data==i&&interestData[i].selected == 0){
			interestData[i].selected = 1
			param.interestKey=interestData[i].name;
		}
		else{
			interestData[i].selected = 0
		}
	}
	gridInterest.items = interestData;
});
gridIndustry.on("touch", function(data) {
	param.industryKey="";
	for(var i in industryData){
		if(data==i&&industryData[i].selected == 0){
			industryData[i].selected = 1
			param.industryKey=industryData[i].val;
		}
		else{
			industryData[i].selected = 0
		}
	}
	gridIndustry.items = industryData;
});
gridDelicacy.on("touch", function(data) {
	param.delicacyKey="";
	for(var i in delicacyData){
		if(data==i&&delicacyData[i].selected == 0){
			delicacyData[i].selected = 1
			param.delicacyKey=delicacyData[i].name;
		}
		else{
			delicacyData[i].selected = 0
		}
	}
	gridDelicacy.items = delicacyData;
});
gridHometown.on("touch", function(data) {
	param.hometownKey="";
	for(var i in hometownData){
		if(data==i&&hometownData[i].selected == 0){
			hometownData[i].selected = 1
			param.hometownKey=hometownData[i].val;
		}
		else{
			hometownData[i].selected = 0
		}
	}
	gridHometown.items = hometownData;
});
gridEntertainment.on("touch", function(data) {
	param.entertainmentKey="";
	for(var i in entertainmentData){
		if(data==i&&entertainmentData[i].selected == 0){
			entertainmentData[i].selected = 1
			param.entertainmentKey=entertainmentData[i].name;
		}
		else{
			entertainmentData[i].selected = 0
		}
	}
	gridEntertainment.items = entertainmentData;
});
ui("cancelBtn").on("touch", function(data) {
	param= {"sexKey":"","interestKey":"","industryKey":"","delicacyKey":"","hometownKey":"","entertainmentKey":""};
	page.fire("refreshInterest",param);
	hideLayout();
});

ui("saveBtn").on("touch", function(data) {
	var paramN= {"sexKey":param.sexKey,"interestKey":param.interestKey,"industryKey":param.industryKey,"delicacyKey":param.delicacyKey,"hometownKey":param.hometownKey,"entertainmentKey":param.entertainmentKey};
	page.fire("refreshInterest",paramN);
	hideLayout();
});
function hideLayout(){
	pickerbox.animate(animPanelHide);
	pickerbg.animate(animMaskHide,function(){
		pickerbg.visible = false;
	});
}